﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess;
using System.Linq.Expressions;
using QuanLyKhachSan.Validation;
using QuanLyKhachSan;
using System.Data.Entity;

namespace QuanLyKhachSan
{
    public class BaoMatManager : IValidator<TaiKhoan>
    {
        public ValidateResult Validate(TaiKhoan obj)
        {
            var vResult = new ValidateResult();
            if (obj.Username.Equals(""))
            {
                vResult.Message.Add("Chưa nhập username");
            }
            if (obj.Password.Equals(""))
            {
                vResult.Message.Add("Chưa nhập password");
            }
            return vResult;
        }

        public ActionResult CapNhat(TaiKhoan obj)
        {
            var actionResult = new ActionResult();
            using (var context = new QLKhachSanContext())
            {
                var tkUpdate = context.TaiKhoans.Single(t => t.Username.Equals(obj.Username));
                try
                {
                    tkUpdate.Username = obj.Username;
                    tkUpdate.Password = obj.Password;
                    context.Entry(tkUpdate).State = EntityState.Modified;
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    actionResult.Exceptions.Add(ex);
                }
            }
            return actionResult;
        }

        public ActionResult CapNhatReset(TaiKhoan obj)
        {
            
            var actionResult = new ActionResult();
            using (var context = new QLKhachSanContext())
            {
                var tkUpdate = context.TaiKhoans.Single(t => t.EmailDangKy.Equals(obj.EmailDangKy));
                try
                {
                    tkUpdate.Password = obj.PasswordTamThoi;
                    context.Entry(tkUpdate).State = EntityState.Modified;
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    actionResult.Exceptions.Add(ex);
                }
            }
            return actionResult;
        }
    }
}
